package com.tgjs.sphoto;

import android.app.Application;
import android.content.Context;
import android.os.StrictMode;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.Utils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.SPCookieStore;
import com.lzy.okgo.https.HttpsUtils;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;

import java.io.InputStream;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.OkHttpClient;

/**
 * Created by dql on 2018/8/9.
 */

public class MyApplication extends Application {
	private static Context mAppContext;

	@Override
	public void onCreate() {
		super.onCreate();
		mAppContext=this;
		initHttpUtil();
		Utils.init(this);
		// android 7.0系统解决拍照的问题
		StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
		StrictMode.setVmPolicy(builder.build());
		builder.detectFileUriExposure();
	}

	private void initHttpUtil() {
		OkHttpClient.Builder builder = new OkHttpClient.Builder();
		HttpsUtils.SSLParams sslParams1 = HttpsUtils.getSslSocketFactory();
		HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
		loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.NONE);
		loggingInterceptor.setColorLevel(Level.INFO);
		builder.addInterceptor(loggingInterceptor);
		builder.cookieJar(new CookieJarImpl(new SPCookieStore(this)));
		builder.connectTimeout(120*1000, TimeUnit.MILLISECONDS);
		builder.sslSocketFactory(sslParams1.sSLSocketFactory, sslParams1.trustManager);
		builder.hostnameVerifier(new HostnameVerifier() {

			@Override
			public boolean verify(String hostname, SSLSession session) {
				//强行返回true 即验证成功
				return true;
			}
		})
				.proxy(Proxy.NO_PROXY);
		OkGo.getInstance().init(this)
				.setOkHttpClient(builder.build())
				//全局统一缓存模式，默认不使用缓存，可以不传
				.setCacheMode(CacheMode.NO_CACHE)
				//全局统一缓存时间，默认永不过期，可以不传
				.setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
				.setRetryCount(5);

		Glide.get(this).register(GlideUrl.class,InputStream.class,new OkHttpUrlLoader.Factory(builder.build()));
	}
}
